#pragma once

#include "State.hpp"
#include "Project.hpp"
#include "Script.hpp"
#include "UiDefinition.hpp"
#include "DocumentWindow.hpp"




namespace Engine
{
	namespace Edit
	{
		class MainArea
			: public QWidget
		{
			Q_OBJECT

		public:

			MainArea(QWidget* parent);
			virtual ~MainArea();



			void setProject(Project::ptr project);

			void changeState(State::Type);

			/**
			 * Tests if this or ANY opened document is modified.
			 */
			bool isWindowModified() const;

			QMdiArea::ViewMode viewMode() const;
			void setViewMode(QMdiArea::ViewMode mode);

		public slots:

			/**
			 * Opens a view that displays the given item.
			 * If there already is a view, it is brought to the top.
			 */
			void show(ProjectItem::ptr item);

			/**
			 * Tries to close the view(s) that displays the given item.
			 * @returns true if there is no view for that item or if the view has been closed
			 * @returns false if the view rejected closing
			 */
			bool close(ProjectItem::ptr item);



			/**
			 * Saves all opened documents.
			 */
			void saveAll();

			/**
			 * Closes all opened documents.
			 */
			void closeAllDocuments();

			/**
			 * @returns a pointer to the active document window, or nullptr
			 */
			DocumentWindow* active() const;

			/**
			 * @returns the number of opened documents
			 */
			std::size_t numDocuments() const;

		protected slots:

			void onSubWindowActivated(QMdiSubWindow* window);
			void onSubWindowClosed(DocumentWindow* window);
			ProjectItem::Result projectModified(ProjectItem::weak_ptr sender, ProjectItem::weak_ptr item, ProjectItem::Action action);

		signals:

			/**
			 * This signal is emitted when the active document has changed.
			 */
			void activeDocumentChanged(DocumentWindow* window);

			/**
			 * This signal is emitted whenever a new window is added to the underlying QMdiArea.
			 */
			void windowAdded(DocumentWindow* window);

			/**
			 * This signal is emitted whenever a window is removed from the underlying QMdiArea.
			 */
			void windowRemoved(DocumentWindow* window);

		private:

			/**
			 * Adds and shows the given window window if the contained QMdiArea.
			 */
			void add(DocumentWindow* window);

		private:

			State::Type    m_state;
			QMdiArea       m_area;
			Project::ptr   m_project;
		};
		///////////////////////////////////////////////////////////////////////////////////////////
	}
	///////////////////////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////////////
